﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using sharpPDF;
using System.Threading;

using MigraDoc;
using PdfSharp;

using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using PdfSharp.Pdf;

namespace ReturnSticks
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AmountBox.Text = "10";
        }

        private void GenerateBtn_Click(object sender, EventArgs e)
        {
            int amount = 0;
            int filial = 0;
            try
            {
                amount = Convert.ToInt16(AmountBox.Text);
            }
            catch
            {
                return;
            }
            string fil = null;
            try
            {
                fil = FilialBox.SelectedItem.ToString();
            }
            catch
            {
                return;
            }
            switch (fil)
            {
                case "Днепропетровск":
            {
                filial = 490;
                break;
            }
                case "Полтава":
            {
                filial = 360;
                break;
            }
                case "Запорожье":
            {
                filial = 690;
                break;
            }
                case "Киев":
            {
                filial = 010;
                break;
            }
            }

            if (amount <= 0 || amount > 1000 || filial == 0)
                return;
            else
            {
                GenerateSticks(filial, amount);
            }
        }

        //Генерирем стики.
        private void GenerateSticks(int filial, int amount)
        {
            //Создаём массив штрихкодов.
            Image[] barcodesArr = new Image[amount];
            double unix = GetUnixTimestamp();
            for (int i = 0; i < amount; i++)
            {
                string data = unix.ToString();
                barcodesArr[i] = GetBarCode(data);
                unix++;
            }

            //Вписываем штрихкод в PDF.
            pdfDocument myDoc = new pdfDocument("TUTORIAL", "ME");
            for (int i = 0; i < amount; i++)
            {
                pdfPage myPage = myDoc.addPage();

                Bitmap bm = new Bitmap(barcodesArr[i]);

                barcodesArr[i].Save(i + ".gif", ImageFormat.Gif);

                Image im = Image.FromFile(i + ".gif");
                myDoc.addImageReference(im, i.ToString() + "num");
                myPage.addImage(myDoc.getImageReference(i.ToString() + "num"), 10, 10);
            }
            myDoc.createPDF("qwerty.pdf");

        }

        //Конвертация в юникс время.
        double GetUnixTimestamp()
        {
            DateTime temp = DateTime.UtcNow;
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            TimeSpan diff = temp - origin;
            double d = Math.Floor(diff.TotalMilliseconds);
            return d;
        }

        //Генерация картинки штрихкода.
        private Image GetBarCode(string data)
        {
            Image bm;

            Ean13Settings settings = new Ean13Settings();
            settings.BarCodeHeight = 150;
            string code = data.Substring(1, 12);
            code = Ean13.CreateChecksum(code);
            string title = "www.panteon.postmarka.com";

            Ean13 barcode = new Ean13(code, title);
            bm = barcode.Paint();

            return bm;
        }
    }
}
